package com.aries.ui.util;

import com.aries.ui.view.title.ColorStateList;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.render.BlendMode;

/**
 * @Author: AriesHoo on 2018/7/19 9:47
 * @E-Mail: AriesHoo@126.com
 * Function: Drawable设置相关工具类
 * Description:
 * 1、2018-11-16 16:36:09 修改newDrawable返回
 * 2、2019-4-22 17:00:49 新增{@link #setTintDrawable(ShapeElement, int)}{ #setTintDrawable(ShapeElement, int[][], int[])}用于动态设置Drawable 颜色
 */
public class DrawableUtil {

    /**
     * 设置drawable宽高
     *
     * @param drawable  drawable
     * @param width  width
     * @param height  height
     */
    public static void setDrawableWidthHeight(Element drawable, int width, int height) {
       // LogUtil.loge("setDrawableWidthHeight "+width+"/"+height);
        try {
            if (drawable != null && !(drawable instanceof VectorElement)) {
                drawable.setBounds(0, 0,
                        width >= 0 ? width : drawable.getWidth(),
                        height >= 0 ? height : drawable.getHeight());
            }
        } catch (Exception e) {
        }
    }

    /**
     * 复制当前drawable
     *
     * @param drawable  drawable
     * @return  Element
     */
    public static Element getNewDrawable(Element drawable) {
        if (drawable == null) {
            return null;
        }
        return drawable.getCurrentElement();
    }

    /**
     * 给一个Drawable变换线框颜色
     *
     * @param drawable 需要变换颜色的drawable
     * @param color    需要变换的颜色
     * @return  ShapeElement
     */
    public static ShapeElement setTintDrawable(ShapeElement drawable, int color) {
        if (drawable != null) {
            drawable.setRgbColor(RgbColor.fromArgbInt(color));
            drawable.setStateColorMode(BlendMode.SRC_ATOP);
        }
        return drawable;
    }

    public static Element setTintDrawable(Element drawable, ColorStateList colors) {
        if (drawable != null && colors.mStateSpecs != null && colors.mColors != null) {
            drawable.setStateColorList(colors.mStateSpecs, colors.mColors);
            drawable.setStateColorMode(BlendMode.SRC_ATOP);
        }
        return drawable;
    }
    
    public static Element setTintMode(Element drawable, BlendMode tintMode, int color) {
        if (drawable != null && tintMode != null) {
            drawable.setStateColorList(new int[0][0], new int[]{color});
            drawable.setStateColorMode(BlendMode.SRC_ATOP);
        }
        return drawable;
    }
}
